package com.bjsxt.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.*;

@Controller
public class IndexController {

    @Value("classpath:init.json")
    private Resource resource;


    @GetMapping("/index")
    public String index(){
        return "index";
    }


    /**
     * 读取resources目录下的init.json目录，以json格式返回菜单
     *
     */
    @GetMapping(value = "/menu")
    @ResponseBody
    public String menu(){
        try {
            File file = resource.getFile();  //返回与指定路径中某文件相应的 File 对象。
//            FileReader fileReader = new FileReader(file);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            String str;
            StringBuffer sb =  new StringBuffer();
            while ((str=bufferedReader.readLine())!=null){
                sb.append(str);
            }
            bufferedReader.close();
            //使用gbk从新编码
            String result = new String(sb.toString().getBytes(),"gbk");
            System.out.println(result);
            return sb.toString();

        } catch (IOException e) {
            e.printStackTrace();
        }
            return null;
    }
}
